import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {
    public static void main(String[] args) {

        //1.创建服务器端的ServerSocket，指明自己的端口号
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(10010);
            while (true) {
                System.out.println("服务器正在等待连接");
                //2.调用accept表示监听客户端的socket
                Socket socket = ss.accept();
                //3.不断监听,不断展示数据,获取通道内的输入流(InputStream)
                InputStream inputStream = socket.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader br = new BufferedReader(inputStreamReader);
                //一次读取一行
                String line = null;
                while ((line = br.readLine()) != null) { //阻塞式方法
                    // //展示数据
                    System.out.println(line);
                }
                //4.获取ip
                String ip = socket.getInetAddress().getHostAddress();
                //展示数据
                System.out.println("data from " + ip + " content is--->" + line);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
